﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DC2010;
using DC2010.Tools;
using DC2010_Editor.Formulars;
using DC2010.Objects.Info;
using DC2010.Effects;
using DC2010_Editor.Properties;
using System.Drawing.Drawing2D;
using DC2010.Objects.Base;

namespace DC2010_Editor.Controls
{
    public partial class EffectUC : UserControl
    {
        private Effect data { get; set; }

        public EffectUC()
        {
            InitializeComponent();
        }

        private void crystalButton3_Click(object sender, EventArgs e)
        {
            ChooseBasedOn chbo = new ChooseBasedOn("Effect");
            chbo.ShowDialog();

            if (!string.IsNullOrEmpty(chbo.ChosenAncessor))
            {
                Effect i = Root.I.IS.GetEffect(chbo.ChosenAncessor);
                Effect iNew = (Effect)i.Clone();

                string id = string.Format(EffectIdTextBox.Text + "_{0}", Editor.I.EffectsInstanceCount);
                string tempId = BasedOnTextBox.Text;


                iNew.Info.Id = id;
                iNew.Info.TemplateId = i.Info.Id;

                Root.I.EffectSystem.AddToSystem(iNew, id, ObjectTarget.Instance);
                Root.I.EffectSystem.SaveInstance();

                Refresh();
            }
        }

        private void crystalButton7_Click(object sender, EventArgs e)
        {
            if (data != null)
            {
                data.Id = data.Info.Id;
                Root.I.EffectSystem.RemoveFromSystem(data, ObjectTarget.Core);
                Root.I.EffectSystem.RemoveFromSystem(data, ObjectTarget.Instance);
            }

            Refresh();
        }

        private void crystalButton5_Click(object sender, EventArgs e)
        {
            if (data != null)
            {
                Root.I.EffectSystem.MoveToC(data.Info.Id);
            }

            Refresh();
        }

        private void crystalButton6_Click(object sender, EventArgs e)
        {
            if (data != null)
            {
                Root.I.EffectSystem.MoveToI(data.Info.Id);
            }

            Refresh();

        }

        private void crystalButton8_Click(object sender, EventArgs e)
        {
            /// Remove from system.
            Root.I.EffectSystem.RemoveFromSystem(data, ObjectTarget.Instance);

            Effect i = Root.I.IS.GetEffect(data.TemplateId);
            Effect iNew = (Effect)i.Clone();

            string id = data.Id;
            string tempId = BasedOnTextBox.Text;

            iNew.Info.Id = id;
            iNew.Info.TemplateId = i.Info.Id;

            Root.I.EffectSystem.AddToSystem(iNew, id, ObjectTarget.Instance);
            Root.I.EffectSystem.SaveInstance();
        }

        private void EffectUC_Load(object sender, EventArgs e)
        {
            Refresh();
        }

        private void Refresh()
        {
            TemplatesImageListBox.Items.Clear();
            DungeonImageListBox.Items.Clear();

            comboBox1.DataSource = null;
            comboBox1.Items.Clear();

            foreach (Effect i in Root.I.EffectSystem.List)
            {
                ImageListBoxItem itm = new ImageListBoxItem(i.Info.Id, Resources.Core, i);
                TemplatesImageListBox.Items.Add(itm);
            }

            foreach (Effect i in Root.I.EffectSystem.ListInstance)
            {
                BaseObject c = i.FindOwner();
                if (c == null)
                {
                    ImageListBoxItem itm = new ImageListBoxItem(i.Info.Id, Resources.Instanced, i);
                    DungeonImageListBox.Items.Add(itm);
                }
                else
                {
                    ImageListBoxItem itm = new ImageListBoxItem(i.Info.Id, Resources.InstancedWithOwner, i);
                    DungeonImageListBox.Items.Add(itm);
                }
            }

            comboBox1.DataSource = Enum.GetValues(typeof(EffectType));
        }

        private void imageListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ImageListBox ilb = (ImageListBox)sender;

            if (ilb.SelectedItem != null)
            {
                if (ilb.SelectedItem is ImageListBoxItem)
                {
                    ImageListBoxItem ilbi = (ImageListBoxItem)ilb.SelectedItem;

                    if (ilbi.Tag is Effect)
                    {
                        data = (Effect)ilbi.Tag;
                        SetControls();

                        if (string.IsNullOrEmpty(data.Info.TemplateId)) pictureBox1.Image = Resources.tick;
                        if (!string.IsNullOrEmpty(data.Info.TemplateId))
                        {
                            if (Root.I.EffectSystem.Dict.ContainsKey(data.Info.TemplateId))
                                pictureBox1.Image = Resources.tick;
                            else
                                pictureBox1.Image = Resources.cross;
                        }
                    }
                }
            }
        }

        public void SetControls()
        {
            //Ids
            BasedOnTextBox.Text = data.Info.TemplateId;
            EffectIdTextBox.Text = data.Info.Id;

            textBox1.Text = data.Info.Params.Param1.ToString();
            textBox2.Text = data.Info.Params.Param2.ToString();
            textBox3.Text = data.Info.Params.Param3.ToString();
            textBox4.Text = data.Info.Params.Param4.ToString();
            textBox5.Text = data.Info.Params.Param5.ToString();

            textBox10.Text = data.Info.Params.sParam1;
            textBox9.Text = data.Info.Params.sParam2;
            textBox8.Text = data.Info.Params.sParam3;
            textBox7.Text = data.Info.Params.sParam4;
            textBox6.Text = data.Info.Params.sParam5;
            comboBox1.SelectedItem = data.EffectType;

            seTextBox1.SetRelatedObject(data.Info.Params.NestedEffects, data.EffectType);
            masteryGainTextBox1.SetRelatedObject(data.Info.Params.AffectedMasteries);
        }

        public void GetFromControls()
        {
            //Ids
            data.Info.TemplateId = BasedOnTextBox.Text;
            data.Info.Id = EffectIdTextBox.Text;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (data != null)
            {
                seTextBox1.SetRelatedObject(data.Info.Params.NestedEffects, (EffectType)data.EffectType);
            }
        }



    }
}
